import http from '@/http';
import { Contest, ContestListParams, ContestRuleType, ContestStatus, VerifyPasswordParams, Announcement, AnnouncementListParams, ContestParticipant, ContestParticipantListParams } from '@/types/contest';

// 排行榜接口定义
interface ContestRankingParams {
  contest_id: number;
  page?: number;
  limit?: number;
}

export const contestService = {
  // Contest APIs
  getContests(params?: ContestListParams) {
    return http.get<Contest[]>('/api/contest/', { params });
  },

  getContest(id: number) {
    return http.get<Contest>(`/api/contest/${id}/`);
  },

  createContest(data: Partial<Contest>) {
    return http.post<Contest>('/api/contest/', data);
  },

  updateContest(id: number, data: Partial<Contest>) {
    return http.patch<Contest>(`/api/contest/${id}/`, data);
  },

  partialUpdateContest(id: number, data: Partial<Contest>) {
    return http.put<Contest>(`/api/contest/${id}/`, data);
  },

  deleteContest(id: number) {
    return http.delete(`/api/contest/${id}/`);
  },

  verifyPassword(id: number, data: VerifyPasswordParams) {
    return http.post(`/api/contest/${id}/verify_password/`, data);
  },

  // 检查用户是否参赛
  checkParticipation(contestId: number) {
    return http.get<{
      has_participated: boolean;
      is_verified: boolean;
      joined_at: string | null;
    }>('/api/contest/participation/check_participation/', {
      params: { contest_id: contestId }
    });
  },

  // Announcement APIs
  getAnnouncements(params?: AnnouncementListParams) {
    return http.get<Announcement[]>('/api/contest/announcement/', { params });
  },

  getAnnouncement(id: number) {
    return http.get<Announcement>(`/api/contest/announcement/${id}/`);
  },

  createAnnouncement(data: Partial<Announcement>) {
    return http.post<Announcement>('/api/contest/announcement/', data);
  },

  updateAnnouncement(id: number, data: Partial<Announcement>) {
    return http.put<Announcement>(`/api/contest/announcement/${id}/`, data);
  },

  deleteAnnouncement(id: number) {
    return http.delete(`/api/contest/announcement/${id}/`);
  },

  // Contest Participants APIs
  getParticipants(params: ContestParticipantListParams) {
    return http.get<ContestParticipant[]>('/api/contest/participation/', { params });
  },

  getParticipant(id: number, ) {
    return http.get<ContestParticipant>(`/api/contest/participation/${id}/`);
  },

  deleteParticipant(id: number) {
    return http.delete(`/api/contest/participation/${id}/`);
  },

  // 获取比赛排名
  getContestRankings(params: ContestRankingParams) {
    return http.get('/api/contest/rank/', { params });
  },

  // 批阅相关API
  // 获取比赛所有提交（分页）
  getContestSubmissions(contestId: number, params?: { page?: number; page_size?: number }) {
    return http.get(`/api/contest/submission_review/`, { 
      params: { contest_id: contestId, ...params } 
    });
  },

  // 删除提交
  deleteSubmission(submissionId: string) {
    return http.delete('/api/contest/submission_review/', { 
      data: { submission_id: submissionId } 
    });
  },

  // 重新评测提交
  rejudgeSubmission(submissionId: string) {
    return http.post(`/api/contest/submission_review/${submissionId}/rejudge_submission/`);
  }
}; 